// 获取应用实例
const app = getApp()
const api = app.globalData.api

Page({
    // 默认数据
    data: {
        waid: '',
        now: '',
        state: [],
        result: [],
        page: 1,
        pageAll: 0,
        where: ''
    },
    // 获取仓库列表
    search: async function () {
        let result = await api.curlGet('?cla=processInspection&fun=search')
        if (result.code === 0) {
            api.showToast(res.warn)
        } else {
            const res = result.data
            this.setData({
                state: res.state,
            })
        }
    },
    // 列表数据
    list: async function (param = {}, page = 1) {
        const url = '?cla=processInspection&fun=list&page=' + page
        let res = await api.curlPost(url, param)
        if (res.code === 1) {
            const page = res.data.page
            this.setData({
                result: res.data.result,
                page: page.pageNow,
                pageAll: page.pageAll,
                where: api.arrayToJson(param)
            })
        } else {
            api.showToast(res.warn)
        }
    },
    onSearch: async function (e) {
        const orderNo = e.detail
        if (orderNo) {
            const state = wx.getStorageSync('state') ? wx.getStorageSync('state') : '待质检'
            this.list({
                orderNo: orderNo,
                state: state
            })
        }
    },
    onChangeState: async function (e) {
        let state = e.detail.name
        if (state) {
            wx.setStorageSync('nowState', state)
            this.list({state: state})
        }
    },
    onScan: async function (e) {
        wx.scanCode({
            onlyFromCamera: true,
            success(res) {
                if (res.errMsg === "scanCode:ok") {
                    const string = res.result
                    let array = api.parseStr(string)
                    if (['processInspectionOther', 'processInspection'].includes(array['source'])) {
                        wx.navigateTo({
                            url: 'add?id=' + array['id'],
                        })
                    }
                }
            }
        })
    },
    goto(e) {
        return api.goto(e)
    },
    main: async function () {
        await this.search()
        await this.list()
    },
    onLoad: async function (options) {
        this.main()
    },
    // 上拉加载
    onReachBottom() {
        if (!this.loading && this.data.page < this.data.pageAll) {
            const page = parseInt(this.data.page),
                param = api.jsonToArray(this.data.where)
            this.list(param, page + 1)
        }
    },
    // 下拉刷新
    onPullDownRefresh() {
        if (!this.loading) {
            const param = api.jsonToArray(this.data.where)
            this.list(param).then(() => {
                // 处理完成后，终止下拉刷新
                wx.stopPullDownRefresh()
            })
        }
    },
    // 撤销
    cancel: async function (obj) {
        let id = obj.currentTarget.dataset.id
        let index = obj.currentTarget.dataset.index
        const url = '?cla=processInspection&fun=cancel'
        let res = await api.curlPost(url, {id: id})
        if (res.code === 1) {
            this.list({state: '已质检'})
        } else {
            api.showToast(res.warn)
        }
      },
});
